<template>
	<!-- 基础用法，不包含校验规则 -->
	<view class="formView">
		<u-picker :show="showPicker" :columns="columns" keyName="label" @cancel="showPicker = false"
			@confirm="confirmPicker"></u-picker>
		<uni-forms ref="baseForm" :modelValue="baseFormData" class="form-Info" :rules="rules">
			<uni-forms-item label="走访对象:" label-width="90px" class="item-label">
				<view class="item-content">
					{{ baseFormData.name }}
				</view>
			</uni-forms-item>
			<uni-forms-item label="类型:" name="visitPeople" label-width="90px" class="item-label">
				<view class="item-content">
					{{ baseFormData.visitPeople }}
				</view>
			</uni-forms-item>
			<template v-if="baseFormData.labelType == 1">
				<view class="" @click="openPicker('specialPopulationsStatus')">
					<uni-forms-item label="状态:" label-width="90px" name="specialPopulationsStatus"
						class="item-label" required>
						<view class="item-content">
							{{ getValue(baseFormData.specialPopulationsStatus, columns[0]) }}
						</view>
					</uni-forms-item>
				</view>
			</template>
			<template v-if="baseFormData.labelType == 2">
				<view class="" @click="openPicker('dbstatusId')" v-if="baseFormData.labelValue == 0">
					<uni-forms-item label="状态:" name="dbstatusId" label-width="90px" class="item-label" required>
						<view class="item-content">
							{{ getValue(baseFormData.dbstatusId, columns[0]) }}
						</view>
					</uni-forms-item>
				</view>
				<view class="" @click="openPicker('oldjtId')" v-if="baseFormData.labelValue == 1">
					<uni-forms-item label="状态:" name="oldjtId" label-width="90px" class="item-label" required>
						<view class="item-content">
							{{ getValue(baseFormData.oldjtId, columns[0]) }}
						</view>
					</uni-forms-item>
				</view>
				<view class="" @click="openPicker('subsidyId')" v-if="baseFormData.labelValue == 2">
					<uni-forms-item label="状态:" name="subsidyId" label-width="90px" class="item-label" required>
						<view class="item-content">
							{{ getValue(baseFormData.subsidyId, columns[0]) }}
						</view>
					</uni-forms-item>
				</view>
			</template>
			<view class="" v-if="baseFormData.labelType =='1'">
				<uni-forms-item class="item-label" prop="familiesName" label-width="110px" label="五包一责任人:" required>
					<u-input v-model="baseFormData.familiesName" class="item-input" />
				</uni-forms-item>
			</view>
			<view class="" v-if="baseFormData.labelType == '1'">
				<uni-forms-item class="item-label" name="familiesMobile" label-width="110px" label="家属联系方式:"
					required>
					<u-input v-model="baseFormData.familiesMobile" />
				</uni-forms-item>
			</view>
			<uni-forms-item label="走访日期:" label-width="90px" class="item-label">
				<view class="item-content">
					{{ baseFormData.visitTime }}
				</view>
			</uni-forms-item>
			<uni-forms-item label="联系方式:" label-width="90px" class="item-label">
				<view class="item-content">
					{{ baseFormData.phone || '' }}
				</view>
			</uni-forms-item>
			<uni-forms-item label="上门时间:" label-width="90px" name="checkDate" class="item-label" required>
				<uni-datetime-picker type="datetime" :end="newDate" v-model="baseFormData.checkDate"
					:border="false" />
			</uni-forms-item>
			<uni-forms-item label="核实情况:" label-width="90px" name="hsMemo" class="item-label" required>
				<u--textarea v-model="baseFormData.hsMemo" placeholder="请输入核实情况" height="80"></u--textarea>
			</uni-forms-item>
			<uni-forms-item label="上传附件:" label-width="90px" name="pict" class="item-label" required>
				<template>
					<view class="item-content">
						<u-upload @afterRead="afterRead" @delete="deletePic" v-model="baseFormData.pict"
							:max-count="3" :auto-upload="true" :file-list="fileList"></u-upload>
					</view>
					<view style="font-size: 12px; color: #a4a9bf">附件支持上传图片、视频、音频等</view>
				</template>
			</uni-forms-item>
			<uni-forms-item label="备注:" label-width="90px" class="item-label">
				<u--textarea v-model="baseFormData.memo" placeholder="请输入内容" height="70"></u--textarea>
			</uni-forms-item>
		</uni-forms>
		<view style="margin:  20px">
			<u-button :ripple="true" type="primary" size="medium" shape="circle" @click="submit">提交</u-button>
		</view>
	</view>
</template>
<script>
	import * as TaskApi from '@/api/task.js'
	import * as DictApi from '@/api/system/dict.js'
	import {
		formatDate
	} from "@/utils/common.js"
	export default {
		data() {
			return {
				newDate: '',
				baseFormData: {
					name: '',
					visitPeople: '',
					memo: '',
					pict: '',
					phone: '',
					hsMemo: '',
					visitTime: '',
					labelType: '',
					familiesName: '',
					familiesMobile: '',
					checkDate: '',

				},
				action: 'https://hjzhjd.greatsea.net.cn/app-api/infra/file/upload',
				fileList: [],
				dictList: [],
				showPicker: false,
				columns: [],
				pickerType: '',
				show: false,
				rules: {
					familiesName: {
						rules: [{
							required: true,
							errorMessage: '请选择五包一责任人'
						}],
					},
					familiesMobile: {
						rules: [{
							required: true,
							errorMessage: '请选择家属联系方式'
						}, {
							validateFunction: function(rule, value, data, callback) {
								let phone = /^1[3|4|5|6|7|8|9][0-9]\d{8}$/
								if (!phone.test(value)) {
									callback('手机号码格式不正确，请重新填写')
								}
								return true
							}
						}],
					},
					checkDate: {
						rules: [{
							required: true,
							errorMessage: '请输入上门时间'
						}],
					},
					specialPopulationsStatus: {
						rules: [{
							required: true,
							errorMessage: '请选择特殊人员状态'
						}],
					},
					dbstatusId: {
						rules: [{
							required: true,
							errorMessage: '请选择低保状态'
						}],
					},
					oldjtId: {
						rules: [{
							required: true,
							errorMessage: '请选择老年人状态'
						}],
					},
					hsMemo: {
						rules: [{
							required: true,
							errorMessage: '核实情况不能为空'
						}],
					},
					pict: {
						rules: [{
							required: true,
							errorMessage: '请上传照片'
						}],
					},
				}

			}
		},
		mounted() {
			setTimeout(() => {
				this.baseFormData.checkDate =formatDate(Date.now())
			}, 500)
		},
		created() {
			this.newDate = formatDate(new Date())
		},
		methods: {
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			async afterRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						// url: 'http://8.148.7.113:48081/app-api/infra/file/upload',
						url: 'https://hjzhjd.greatsea.net.cn/app-api/infra/file/upload',
						filePath: url,
						name: 'file',
						formData: {
							// user: 'test'
						},
						success: (res) => {
							console.log('88', JSON.parse(res.data).data)
							setTimeout(() => {
								resolve(res.data.data)
								this.baseFormData.pict = JSON.parse(res.data)
									.data
							}, 1000)
						}
					});
				})
			},
			getValue(value, list) {
				if (!list || list.length == 0) return ''
				const index = list.findIndex((ele) => ele.value == value)
				return index > -1 ? list[index].label : ''
			},
			getTypeList(value) {
				if (!value) return
				DictApi.getTypeList({
					dictTypeList: [value]
				}).then((res) => {
					this.columns = [res.data[value]]
				})
			},
			confirmPicker(e) {
				this.showPicker = false
				const item = e.value[0]
				this.baseFormData[this.pickerType] = item.value
			},
			openPicker(e) {
				this.showPicker = true
				this.pickerType = e
			},
			submit() {
				const data = this.baseFormData
				this.$refs.baseForm
					.validate()
					.then((res) => {
						if (this.baseFormData.labelType == 1) {
							TaskApi.saveTaskSpecial(data).then(() => {
								uni.switchTab({
									url: '/pages/task/index'
								})
							})

						} else if (this.baseFormData.labelType == 2) {
							TaskApi.saveTaskPool(data).then(() => {
								uni.switchTab({
									url: '/pages/task/index'
								})
								uni.$u.toast('成功提交')
							})
						}
					})
					.catch((errors) => {
						uni.$u.toast('校验失败')
					})
			}
		},
		onLoad(option) {
			const params = {
				residentId: option.residentId,
				taskAssignId: option.id
			}
			TaskApi.getTaskDetail(params).then((res) => {
				const data = res.data
				const dictList = [
					[],
					['psychopath', 'drug_person_status', 'disimprisoned_type', 'zjry_type',
						'xfry_type'
					],
					['dbstatus_type', 'oldJt_type', 'cjrjt_type', 'poor_type']
				]
				this.getTypeList(dictList[data.labelType][data.labelValue])
				this.baseFormData = {
					dbstatusId: res.data.dbstatusId,
					hsMemo: res.data.hsMemo,
					labelType: res.data.labelType,
					labelValue: res.data.labelValue,
					location: res.data.location,
					memo: res.data.memo,
					name: res.data.name,
					phone: res.data.phone,
					pict: res.data.pict,
					roomId: res.data.roomId,
					taskAssignId: res.data.taskAssignId,
					visitPeople: res.data.visitPeople,
					visitTime: res.data.visitTime,
					checkDate: res.data.checkDate,
				}
			})
		}
	}
</script>
<style lang="scss">
	.formView {
		margin: 15px;
	}

	.uni-forms-item {
		margin-bottom: 5px !important;
		border-bottom: 1px solid #b1b1b1
	}

	.uni-forms-item__label {
		font-size: 14px !important;

	}

	.uni-forms-item__content {}

	.item-label {
		font-size: 14px;
		font-weight: 400;
	}

	.item-input {}

	.item-content {
		line-height: 36px;
		font-size: 14px;

	}

	.u-border {
		border-style: none;
	}


	// .uni-forms-item__content{
	// 	border-bottom: 1px solid #00aa7f;
	// }
</style>